package com.xing.libao.init;

import com.xing.libao.util.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/8/7
 * Time: 14:37
 */
public class Feng {
    public static void main(String[] args) {
        Feng feng = new Feng();
        feng.checkBJ();
    }

    private void checkBJ(){
        RequestUtil requestUtil = RequestUtil.getInstance();
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(3000)
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .build();
        for(int i=1;i<11;i++) {
            String content = requestUtil.getHtml("http://www.kuaidaili.com/proxylist/" + i + "/");
            if(StringUtils.isNotEmpty(content)) {
                Document document = Jsoup.parse(content);
                Elements elements = document.select("tbody>tr");
                HttpGet get = new HttpGet("http://www.aibj.biz/?fromuid=454217");
                for (Element element : elements) {
                    Elements my = element.getElementsByTag("td");
                    String ip = my.get(0).text();
                    String port = my.get(1).text();
                    if (StringUtils.isNumeric(port)) {
                        int porti = Integer.parseInt(port);
                        System.out.println(ip + " --- " + porti);
                        HttpHost proxy = new HttpHost(ip, porti);
                        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
                        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
                                .setRoutePlanner(routePlanner)
                                .build();
                        try {
                            CloseableHttpResponse response = httpClient.execute(get);
                            HttpEntity re = response.getEntity();
                            content = EntityUtils.toString(re, "utf-8");
                            System.out.println(content);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
//        HttpGet get = new HttpGet("http://www.aibj.biz/?fromuid=454217");
//        get.setHeader("Upgrade-Insecure-Requests","1");
//        get.setHeader("Host","www.aibj.biz");
//        get.setHeader("Cookie","nzrE_2132_saltkey=U777GxMh; nzrE_2132_lastvisit=1441526301; nzrE_2132_atarget=1; nzrE_2132_auth=4881uolTydO3w0N0te3V1QTIJS0cHH7waQqXb7rN4L0tJT2LMAr8gWk636IViGNp1FxY34tDyFYSFofg5acLVWf4FqE; nzrE_2132_lastcheckfeed=454217%7C1441529910; key302=f5472ae896; expire302=1441683859; nzrE_2132_st_t=454217%7C1441683799%7C16443522d56f48c6254493b4d3b96b3a; nzrE_2132_forum_lastvisit=D_137_1441683799; nzrE_2132_ulastactivity=5e26QFqO5V2vqROqEOjWD3RB3W8DzE1BdMQg%2Bos%2BFzqlgRUh7I6X; nzrE_2132_ignore_notice=1; nzrE_2132_nofavfid=1; nzrE_2132_promotion=309835; nzrE_2132_st_p=454217%7C1441694765%7Cf3d9ebacc37d0e6373eeb4bb4a9a1c6f; nzrE_2132_viewid=tid_434925; nzrE_2132_smile=1D1; nzrE_2132_seccode=30366.591c274e0a6e6f6f94; nzrE_2132_lip=111.205.130.62%2C1441695319; nzrE_2132_sid=BXf9Xu; CNZZDATA5331698=cnzz_eid%3D892808252-1441528985-%26ntime%3D1441692248; nzrE_2132_checkpm=1; nzrE_2132_lastact=1441695634%09home.php%09misc; nzrE_2132_sendmail=1; nzrE_2132_noticeTitle=1");
//        get.setHeader("Connection","keep-alive");
//        System.out.println(requestUtil.getHtml(get));
    }

    private void list2(){
        String url = "http://www.91sizu.net";
        RequestUtil requestUtil = RequestUtil.getInstance();
        System.out.println(requestUtil.getHtml(url));
    }

    private void detail(){
        String url = "http://www.dushitiyan.com/ViewShop.aspx?id=6288";
        RequestUtil requestUtil = RequestUtil.getInstance();
        System.out.println(requestUtil.getHtml(url));
    }

    private void list(){
        String url = "http://www.dushitiyan.com/ShopList.aspx?district=125";
        RequestUtil requestUtil = RequestUtil.getInstance();
        System.out.println(requestUtil.getHtml(url));
    }
}
